import React from 'react';
import {Spin, Card, Button, Table, Row, Col, Popover} from 'antd';
import DescriptionList from 'ant-design-pro/lib/DescriptionList';
import PropTypes from 'prop-types';
import {partClaimStatusHis} from '../Enum';
import routes from './routes';
import {TABLE, DATETIME_FORMAT, DATE_FORMAT, FILES_API} from '../constants';
import {conventEnumValueToString, formatDateTime, formatAmount} from '../utils';
import {getDetailHis} from './api';
import {injectIntl} from './intl';
const {Description} = DescriptionList;
class HisDetailPanel extends React.PureComponent {
    state = {
        isFetching: false,
        data: {},
        details: []
    }
    componentDidMount() {
        this.getDetailInfo();
    }
    getDetailInfo = () => {
        const id = this.props.id;
        this.setState({isFetching: true});
        getDetailHis(id)
            .then(res => {
                if(res.ok)
                    this.setState({
                        isFetching: false,
                        data: res.data,
                        details: res.data.details,
                        processDetails: res.data.processDetails
                    });
                else
                    this.setState(({
                        isFetching: false
                    }));
            });
    }
    onCancel = () => {
        this.props.history.push(routes.hisOrder.url());
    }
    render() {
        const {data, processDetails} = this.state;
        const {formatMessage} = this.props.intl;
        const colums = [{
            title: formatMessage({
                id: 'hisDetailPanel.column.partCode',
                defaultMessage: '备件编号'
            }),
            dataIndex: 'partCode',
        }, {
            title: formatMessage({
                id: 'hisDetailPanel.column.partName',
                defaultMessage: '备件名称'
            }),
            dataIndex: 'partName',
        }, {
            title: formatMessage({
                id: 'hisDetailPanel.column.partEngName',
                defaultMessage: '备件英文名'
            }),
            dataIndex: 'partEngName',
        }, {
            title: formatMessage({
                id: 'hisDetailPanel.column.damagedQty',
                defaultMessage: '损坏数量'
            }),
            dataIndex: 'damagedQty',
        }, {
            title: formatMessage({
                id: 'hisDetailPanel.column.price',
                defaultMessage: '备件单价'
            }),
            dataIndex: 'price',
            render: text => formatAmount(text)
        }, {
            title: formatMessage({
                id: 'hisDetailPanel.column.fobAmount',
                defaultMessage: 'FOB金额'
            }),
            dataIndex: 'fobAmount',
            render: text => formatAmount(text)
        }, {
            title: formatMessage({
                id: 'hisDetailPanel.column.claimAmount',
                defaultMessage: '索赔金额'
            }),
            dataIndex: 'claimAmount',
            render: text => formatAmount(text)
        }, {
            title: formatMessage({
                id: 'hisDetailPanel.column.approvalAmount',
                defaultMessage: '批准金额'
            }),
            dataIndex: 'approvalAmount',
            render: text => formatAmount(text)
        }, {
            title: formatMessage({
                id: 'hisDetailPanel.column.claimInvNo',
                defaultMessage: '索赔发票编号'
            }),
            dataIndex: 'claimInvNo'
        }, {
            title: this.props.intl.formatMessage({
                id: 'commonDetail.column.action',
                defaultMessage: '操作'
            }),
            dataIndex: 'action',
            render: (text, record) => {
                const {attachments = []} = record;
                if(!attachments.length)
                    return '';
                const fileList = attachments.map(item => (<Description key={item.fileId}><a icon="download" href={`${FILES_API.files}/${item.fileId}`} size="small">
                    {item.fileName}</a></Description>));
                return (<Popover trigger="click" content={<DescriptionList col={1} size="large">
                    {fileList}
                </DescriptionList>}>
                    <a icon="download" size="small">
                        {this.props.intl.formatMessage({
                            id: 'add.downloadFile',
                            defaultMessage: '下载附件'
                        })}</a>
                </Popover>);
            }
        }];
        
        const processColums = [{
            title: formatMessage({
                id: 'hisDetailPanel.column.processer',
                defaultMessage: '操作人'
            }),
            dataIndex: 'processer',
        }, {
            title: formatMessage({
                id: 'hisDetailPanel.column.processStatus',
                defaultMessage: '操作状态'
            }),
            dataIndex: 'processStatus',
            render: text => conventEnumValueToString(partClaimStatusHis, text)
        }, {
            title: formatMessage({
                id: 'hisDetailPanel.column.processComment',
                defaultMessage: '操作意见'
            }),
            dataIndex: 'processComment',
        }, {
            title: formatMessage({
                id: 'hisDetailPanel.column.processDate',
                defaultMessage: '操作时间'
            }),
            dataIndex: 'processDate',
            render: text => formatDateTime(text, DATETIME_FORMAT)
        }];
        return (
            <div>
                <Spin spinning={this.state.isFetching}>
                    <Card>
                        <DescriptionList title={<Row><Col span={6}>{
                            formatMessage({
                                id: 'hisDetailPanel.card.partBackOrder',
                                defaultMessage: '订单信息'
                            })
                        }</Col>
                        </Row>}>
                            <Description term={formatMessage({
                                id: 'hisDetailPanel.operation.code',
                                defaultMessage: '索赔单号'
                            })}>{data.code}
                            </Description>
                            {!this.props.isDealerUser &&
                                <Description term={formatMessage({
                                    id: 'hisDetailPanel.operation.dealerCode',
                                    defaultMessage: '总代编号'
                                })}>{data.dealerCode}
                                </Description>
                            }
                            {!this.props.isDealerUser &&
                                <Description term={formatMessage({
                                    id: 'hisDetailPanel.operation.dealerName',
                                    defaultMessage: '总代名称'
                                })}>{data.dealerName}
                                </Description>
                            }
                            <Description term={formatMessage({
                                id: 'hisDetailPanel.operation.buyerAddress',
                                defaultMessage: '买方地址'
                            })}>{data.buyerAddress}
                            </Description>
                            <Description term={formatMessage({
                                id: 'hisDetailPanel.operation.buyerTelephone',
                                defaultMessage: '买方联系人'
                            })}>{data.buyerTelephone}
                            </Description>
                            <Description term={formatMessage({
                                id: 'hisDetailPanel.operation.buyerFax',
                                defaultMessage: '买方电话'
                            })}>{data.buyerFax}
                            </Description>
                            <Description term={formatMessage({
                                id: 'hisDetailPanel.operation.buyerContact',
                                defaultMessage: '买方传真'
                            })}>{data.buyerContact}
                            </Description>
                            <Description term={formatMessage({
                                id: 'hisDetailPanel.operation.submitDate',
                                defaultMessage: '提报日期'
                            })}>{formatDateTime(data.submitDate, DATE_FORMAT)}
                            </Description>
                            <Description term={formatMessage({
                                id: 'hisDetailPanel.operation.receiveDate',
                                defaultMessage: '接收日期'
                            })}>{formatDateTime(data.receiveDate, DATE_FORMAT)}
                            </Description>
                            <Description term={formatMessage({
                                id: 'hisDetailPanel.operation.claimAmount',
                                defaultMessage: '索赔金额'
                            })}>{formatAmount(data.claimAmount)}
                            </Description>
                            <Description term={formatMessage({
                                id: 'hisDetailPanel.operation.approvalAmount',
                                defaultMessage: '批准金额'
                            })}>{formatAmount(data.approvalAmount)}
                            </Description>
                        </DescriptionList>
                    </Card>
                    <Card>
                        <DescriptionList title={formatMessage({
                            id: 'hisDetailPanel.table.details',
                            defaultMessage: '清单'
                        })}>
                            <Table
                                className="white-space-nowrap"
                                columns={colums}
                                rowKey="partId"
                                pagination={false}
                                dataSource={this.state.details}
                                {...TABLE} />
                        </DescriptionList>
                    </Card>
                    <Card>
                        <DescriptionList title={formatMessage({
                            id: 'hisDetailPanel.table.processDetails',
                            defaultMessage: '操作历史'
                        })}>
                            <Table
                                className="white-space-nowrap"
                                columns={processColums}
                                rowKey="id"
                                pagination={false}
                                dataSource={processDetails}
                                {...TABLE} />
                        </DescriptionList>
                    </Card>
                </Spin>
                <div className="page-toolbar">
                    <Button type="primary" onClick={this.onCancel}>{this.props.intl.formatMessage({
                        id: 'hisDetailPanel.operation.onCancel',
                        defaultMessage: '返回'
                    })}</Button>
                </div>
            </div>
        );
    }
}

HisDetailPanel.propTypes = {
    history: PropTypes.object,
    id: PropTypes.string,
    intl: PropTypes.object,
    isDealerUser: PropTypes.bool
};

import {connect} from 'react-redux';

const mapStateToProps = state => ({
    isDealerUser: state.getIn((['page', 'domainData', 'initData', 'isDealerUser']))
});
export default connect(mapStateToProps, null)(injectIntl(HisDetailPanel));
